package com.lenovo.dll.nebula.vod.parse.handler;

import java.io.IOException;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import com.lenovo.dll.nebula.vod.parse.service.IPostService;
import com.lenovo.dll.nebula.vod.parse.service.PostService;

public abstract class SaxHandler<T> extends DefaultHandler {

	public void initParse(Map<String,String> params, String strUrl) 
	throws ParserConfigurationException, SAXException, IOException {
		SAXParserFactory spf = SAXParserFactory.newInstance();
		IPostService postService = null;
		try{
			SAXParser sp = spf.newSAXParser();
			XMLReader xmlReader = sp.getXMLReader();
			xmlReader.setContentHandler(this);
			postService = new PostService();
			InputSource source = new InputSource(postService.doPost(params, strUrl));
			// source.setEncoding("UTF-8");
			xmlReader.parse(source);
			
		}finally {
			if(postService != null)
				postService.releaseConnection();
		}		
	}
}
